/*
 * File:   main.cpp
 * Author: f10-kafedra
 *
 * Created on 6 Сентябрь 2011 г., 10:12
 */

//#define NO_LOCALNET

//#define DEBUG_MAIN
//#define DEBUG_REG
//#define DEBUG_INFO
//#define DEBUG_REREG
#define DEBUG_ISBN

#define DEBUG_OUTPUT

#include <QtGui/QApplication>
#include <QTextCodec>
#include <QtNetwork/QHostAddress>
#include <QtNetwork/QNetworkInterface>
#include <QDesktopWidget>
#include <QMessageBox>
#include <QRegExp>
//#include <QtCore/qtextcodec.h>

//#ifdef DEBUG_MAIN
#include "mainForm.h"
//#endif
#ifdef DEBUG_REG
#include "form_UserRegistration.h"
#endif
#ifdef DEBUG_REREG
#include "form_UserReRg.h"
#endif
#ifdef DEBUG_INFO
#include "form_Information.h"
#endif
#ifdef DEBUG_ISBN
#include "form_BookWork.h"
#endif

#include "bdOper.h"

#define REGUSER_VER "1.0"
#define REREGUSER_VER "1.0"
#define INFO_VER "0.7"



QString getMyIp(); // получаем IP адресс

int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    QApplication app(argc, argv);
    app.addLibraryPath(qApp->applicationDirPath() + "/plugins");
//    foreach (QString str, app.libraryPaths())
//    {
//        cout << str.toAscii().data() << endl;
//    }
    
    QTextCodec *codecLocale = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codecLocale);
    QTextCodec *codecStr = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codecStr);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("cp1251"));
    
    //getMyIp();

    QDesktopWidget *d = QApplication::desktop();
    int cur_x = d->width();
    int cur_y = d->height();
    
    
#ifndef DEBUG_ISBN 
//#ifndef DEBUG_REREG
    mainForm mainDebug;
    //mainDebug.started = false;
//#endif
#endif
    bdOper bd;
    bd.myIp = getMyIp();    


#ifdef DEBUG_MAIN    
    //mainDebug.show();
#endif
#ifdef DEBUG_INFO
    mainDebug.apm = APM_INFO;
    form_Information formInfo(QString("%1").arg(argv[0])/*.remove(QRegExp("(.*\\\\)"))*/, mainDebug.client, bd.getFilialNames(bd.getFilialsID()));
    formInfo.setWindowTitle(formInfo.windowTitle() + " [v." + INFO_VER + "]");
    formInfo.show();
#endif    
#ifdef DEBUG_REG    
    mainDebug.apm = APM_REGUSER;
    form_UserRegistration formUsrReg(bd.getFilialNames(bd.getFilialsID()), bd.myIp);
    formUsrReg.setWindowTitle(formUsrReg.windowTitle());
    formUsrReg.move(cur_x / 2 - formUsrReg.width() / 2, 140);
    
//    formUsrReg.client = mainDebug.client;
    formUsrReg.show();
//    form_UserReRg formReReg;
//    formReReg.show();
#endif
#ifdef DEBUG_REREG
    mainDebug.apm = APM_REREG;
    //mainDebug.client->apm = APM_REREG;

#ifdef NO_LOCALNET
    QStringList listFilials;
    listFilials << "ф. 10";
    form_UserReRg formUsrReReg(mainDebug.client, listFilials);
    formUsrReReg.show();
#else    
    form_UserReRg formUsrReReg(bd.getFilialNames(bd.getFilialsID()));
    formUsrReReg.show();
#endif // NO_LOCALNET
#endif // DEBUG_REREG
    
#ifdef DEBUG_ISBN
    form_BookWork formBookSearch;
    formBookSearch.move(cur_x / 2 - formBookSearch.width() / 2, 140);
    formBookSearch.show();
    
#endif
    // create and show your widgets here

    return app.exec();
}

QString getMyIp()
{
    QList<QHostAddress> myIpAddresses = QNetworkInterface::allAddresses();
    
    QRegExp re;
    re.setPattern("\\d{3}.\\d{3}.9.(.*)");
    for (int i = 0; i< myIpAddresses.count(); i++)
    {
        if (re.indexIn(myIpAddresses.at(i).toString()) != -1)
        {
            return re.cap(0);
            break;
        }
    }
    
    return "";
}